package org.iWMS.core.web;

/**
 * Created with IntelliJ IDEA.
 * User: issuser
 * Date: 13-5-2
 * Time: 下午3:13
 * To change this template use File | Settings | File Templates.
 */
import java.util.*;

import javax.servlet.http.HttpServletRequest;


import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.iWMS.core.domain.User;
import org.iWMS.core.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

/**
 * Created with IntelliJ IDEA.
 * User: issuser
 * Date: 13-1-25
 * Time: 下午3:09
 * To change this template use File | Settings | File Templates.
 */
@Controller
public class LoginController{
    private final static Logger logger=Logger.getLogger(LoginController.class);
    private static final String USER_KEY = "user";
    @Autowired
    private UserService userService;

    @Autowired
    private MessageSource messageSource;

    @RequestMapping(value = "/index")
    public String loginPage(){
        return "core/login";
    }

    @RequestMapping(value = "/main")
    public String welcomePage(){
        return "core/main";
    }

    @RequestMapping(value = "/loginCheck")
    public ModelAndView loginCheck(HttpServletRequest request,LoginCommand loginCommand){
        /*
        Object[] params = {loginCommand.getUserName(), new GregorianCalendar().getTime()};
        logger.info(messageSource.getMessage("greeting.morning",params, Locale.CHINA));*/
        UsernamePasswordToken token = new UsernamePasswordToken(loginCommand.getUserName(), loginCommand.getPassword());
        Subject currentUser = SecurityUtils.getSubject();
        try {
            currentUser.login(token);
        } catch (UnknownAccountException uae) {

            return new ModelAndView("login", "error", "用户名不存在。");

        } catch (IncorrectCredentialsException ice) {

            return new ModelAndView("login", "error", "密码错误。");

        } catch ( AuthenticationException ae ) {
            return new ModelAndView("login", "error", "用户名或密码错误。");
        }
        logger.info("用户" + loginCommand.getUserName() + "登录");
        User user = userService.findUserByUserName(loginCommand.getUserName());
        user.setLastIp(request.getLocalAddr());
        user.setLastVisit(new Date());
        userService.loginSuccess(user);
        Session session = currentUser.getSession(true);
        session.setAttribute(USER_KEY, user);
        //动态更改i18N的语言类型
        //session.setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, Locale.US);
        return new ModelAndView("redirect:main.html");
    }

    @RequestMapping(value = "/logout")
    public String logout(){
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession(true);
        User user = (User)session.getAttribute(USER_KEY);
        logger.info("用户" + user.getUserName() + "退出");
        currentUser.logout();
        return "redirect:index.html";
    }
}
